package com.crm.service.impl.orderManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.orderManagerCustomMapper.ProductOrderDetailMapper;
import com.crm.dao.mapper.orderManager.CustomerProductOrderDetailMapper;
import com.crm.model.entity.customerManager.CustomerProductDetail;
import com.crm.model.entity.orderManager.CustomerProductOrderDetail;
import com.crm.model.entity.orderManager.CustomerProductOrderDetailExample;
import com.crm.model.vo.customerManager.CustomerOrderBfhkVO;
import com.crm.model.vo.orderManager.OrderActivityVO;
import com.crm.model.vo.orderManager.OrderDetailDelVO;
import com.crm.model.vo.returnMoney.FinanceProductVO;
import com.crm.model.vo.sendRoyaltyManager.SendRoyaltyProductVO;
import com.crm.service.orderManager.CustomerProductOrderDetailDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/3/17 14:39
 */
@Service
public class CustomerProductOrderDetailDbServiceImpl implements CustomerProductOrderDetailDbService {
    //自动生成的
    @Autowired
    CustomerProductOrderDetailMapper customerProductOrderDetailMapper;

    @Autowired
    ProductOrderDetailMapper orderDetailMapper;

    /**
     * 根据产品订单表主键和订单明细类型查询产品订单明细列表
     * @Author pym
     * @Date  2021/3/17 14:47
     * @param productOrderId -产品订单表主键
     * @param productOrderDetailType -订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:查询产品状态为正常的
     * @return {@link {java.util.List<com.crm.model.entity.orderManager.CustomerProductOrderDetail>}}
     */
    @Override
    public List<CustomerProductOrderDetail> selectCustomerProductOrderDetailList(String productOrderId, String productOrderDetailType) {
        CustomerProductOrderDetailExample customerProductOrderDetailExample = new CustomerProductOrderDetailExample();
        CustomerProductOrderDetailExample.Criteria criteria = customerProductOrderDetailExample.createCriteria();
        customerProductOrderDetailExample.setOrderByClause("create_tm desc");
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());
        if("4".equals(productOrderDetailType)) {
            criteria.andProductOrderDetailTypeEqualTo("1");  //产品类型
            criteria.andProductStatusEqualTo("0");  //正常的
        }else{
            criteria.andProductOrderDetailTypeEqualTo(productOrderDetailType);
        }
        return customerProductOrderDetailMapper.selectByExample(customerProductOrderDetailExample);
    }

    @Override
    public Boolean insertCustomerProductOrderDetail(CustomerProductOrderDetail customerProductOrderDetail) {
        int num = customerProductOrderDetailMapper.insertSelective(customerProductOrderDetail);
        return num>0;
    }

    /**
     * 删除订单产品
     * @param delVo
     */
    @Override
    public void delOrderDetail(OrderDetailDelVO delVo) {
        orderDetailMapper.delOrderDetail(delVo);
    }

    /**
     * 根据订单标号查询退款记录
     * @return
     */
    @Override
    public List<OrderDetailDelVO> getDelOrderProductLogList(String orderId) {
        return orderDetailMapper.getDelOrderProductLogList(orderId);
    }

    /**
     * 根据类型查询订单产品
     * @param productOrderId
     * @param type
     * @return
     */
    @Override
    public List<FinanceProductVO> getOrderProductList(String productOrderId, Integer type) {
        return orderDetailMapper.getOrderProductList(productOrderId,type);
    }

    /**
     * 根據訂單id獲取訂單產品和活動信息
     * @param productOrderId
     * @return
     */
    @Override
    public List<SendRoyaltyProductVO> financeOrderProductAndActivity(String productOrderId, Integer type) {
        return orderDetailMapper.financeOrderProductAndActivity(productOrderId,type);
    }

    /**
     * 获取订单产品、活动数量
     * @param productOrderId
     * @return
     */
    @Override
    public Integer getOrderProductNum(String productOrderId) {
        return orderDetailMapper.getOrderProductNum(productOrderId);
    }

    /**
     * 通过产品明细id查询产品信息
     * @param productOrderDetailId 产品明细id
     * @return
     */
    @Override
    public CustomerProductOrderDetail getCustomerProductOrderDetailById(String productOrderDetailId) {
        return customerProductOrderDetailMapper.selectByPrimaryKey(productOrderDetailId);
    }

    /**
     * 修改产品明细的产品状态为退款
     * @param customerProductOrderDetail 产品明细类
     * @return
     */
    @Override
    public Boolean updateCustomerProductOrderDetail(CustomerProductOrderDetail customerProductOrderDetail) {
        Boolean result = false;
        int num = customerProductOrderDetailMapper.updateByPrimaryKeySelective(customerProductOrderDetail);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过订单id查询订单明细表
     * @param productOrderId 订单id
     * @param productOrderDetailType 订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型
     * @return
     */
    @Override
    public List<CustomerProductOrderDetail> getOrderDetailList(String productOrderId,String productOrderDetailType) {
        CustomerProductOrderDetailExample customerProductOrderDetailExample = new CustomerProductOrderDetailExample();
        CustomerProductOrderDetailExample.Criteria criteria = customerProductOrderDetailExample.createCriteria();
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andProductOrderDetailTypeEqualTo(productOrderDetailType);
        criteria.andProductStatusEqualTo("0");
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());
        return customerProductOrderDetailMapper.selectByExample(customerProductOrderDetailExample);

    }

    /**
     * 查询活动产品数量
     * @param orderDetailId
     * @return
     */
    @Override
    public OrderActivityVO selectActivityProductCount(String orderDetailId) {
        return orderDetailMapper.selectActivityProductCount(orderDetailId);
    }

    /**
     * 通过订单id查询部分回款订单下的产品
     * @param productOrderId 订单id
     * @return
     */
    @Override
    public List<CustomerOrderBfhkVO> selectCustomerOrderBfhkVOList(String productOrderId) {
        return orderDetailMapper.selectCustomerOrderBfhkVOList(productOrderId);
    }

    /**
     * 通过订单产品明细id查询订单明细信息
     * @param productOrderDetailId 订单产品明细id
     * @return
     */
    @Override
    public CustomerProductOrderDetail selectProductOrderDetailList(String productOrderDetailId) {
        return customerProductOrderDetailMapper.selectByPrimaryKey(productOrderDetailId);
    }

    /**
     * 通过订单id、产品id、产品类型查询
     * @param productOrderId
     * @param orderRelationId
     * @param productOrderDetailType
     * @return
     */
    @Override
    public CustomerProductOrderDetail selectProductOrderDetailByOrderIdAndProductId(String productOrderId, String orderRelationId, String productOrderDetailType) {
        return orderDetailMapper.selectProductOrderDetailByOrderIdAndProductId(productOrderId,orderRelationId,productOrderDetailType);
    }

    @Override
    public Integer selectOrderProductCount(String orderId) {
        return orderDetailMapper.selectOrderProductCount(orderId);
    }
}
